/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.pclong.fightlandlordserver;

import java.io.File;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.io.monitor.FileAlterationListenerAdaptor;

import org.apache.commons.io.monitor.FileAlterationObserver;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;

/**
 *  
 * @author long@7road.com
 */
public class FileListener extends FileAlterationListenerAdaptor {

    @Override
    public void onDirectoryChange(File directory) {

        System.out.println("文件目录变更了:" + directory.getAbsolutePath());

    }

    @Override
    public void onDirectoryCreate(File directory) {

        System.out.println("文件目录创建了:" + directory.getAbsolutePath());

    }

    @Override
    public void onDirectoryDelete(File directory) {

        System.out.println("文件目录删除了:" + directory.getAbsolutePath());

    }

    @Override
    public void onFileChange(File file) {

        if (file.getName().endsWith(".xml")) {
            String filename = file.getAbsolutePath();
            System.out.println(filename);
            Serializer serializer = new Persister();
            try {
                if (filename.equals(ConfigFileManager.getInstance().TankAgentConfigFile)) {
                    TankAgentConfigEntity.setInstance(serializer.read(TankAgentConfigEntity.class, file));
//                    System.out.println("重新加载了TankAgentConfigEntity:<Theme>为" + TankAgentConfigEntity.getInstance().Theme);
                } 

            } catch (Exception ex) {
                Logger.getLogger(FileListener.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
        String dateString = "";
        try {
            dateString = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        } catch (Exception e) {
        }

        System.out.println(dateString + " 文件变更了:" + file.getAbsolutePath());

    }

    @Override
    public void onFileCreate(File file) {

        System.out.println("文件创建了:" + file.getAbsolutePath());

    }

    @Override
    public void onFileDelete(File file) {

        System.out.println("文件删除了:" + file.getAbsolutePath());

    }

    @Override
    public void onStart(FileAlterationObserver observer) {
//        System.out.println("开始监听:" + observer.getDirectory());
    }

    @Override
    public void onStop(FileAlterationObserver observer) {
//        System.out.println("停止监听:" + observer.getDirectory());
    }
}
